import random

"""
开户辅助函数
"""

#生成一个不存在的用户名
def product_user_name():
    user_name = random.randint(10000000,100000000)
    while (check_user_name(user_name)):
        user_name = random.randint(10000000, 100000000)
    return user_name

#查找用户名是否存在 存在返回True 不存在返回False
def check_user_name(user_name,dict = bank):
    for i in dict.keys():
        if dict[i]["user_name"] == user_name: #账号存在
            return True
    return False

#判断银行是否存满100个人
def isFull():
    if len(bank)>=100:
        return True
    return False

#插入银行库 1:插入成功,2:用户已存在,3:银行用户库已满
def insert_user(name,address):
    if len(bank)>=100:
        return 3 #银行用户库已满

    user_name = product_user_name()
    user_passwprd = 123456
    account_type = 2
    idx=len(bank) #银行用户库key

    bank[idx] = \
        {
            "user_name": user_name,
            "user_password": user_passwprd,
            "account_type": account_type,
            "name": name,
            "address": address,
            "balance": 0,
            "bank": "中国农业银行"
        }
    bool = check_user_name(user_name)
    if bool :
        serch_user_name(user_name,user_passwprd)
        return 1 #插入成功
    else:
        return 2 #用户已存在

#根据user_name 查询该用户在bank银行用户库中的key值为idx
def serchUserIdxByUser_name(user_name,dict=bank):
    if check_user_name(user_name):
        # 遍历银行用户库查找该用户的idx
        for idx in dict.keys():
            if dict[idx]["user_name"] == user_name:
                return idx
    else:
        return 0 #用户不存在

# print(serchUserIdxByUser_name(11111111))

#判断账户和密码是否匹配 匹配返回True 不匹配返回False
def match_user_name_and_user_password(user_name,user_password,dict = bank):
    idx = serchUserIdxByUser_name(user_name)
    password = dict[idx]["user_password"]
    if user_password == password:
        return True
    else:
        return False


"""
账户查询功能（账号，密码）无返回值
"""
def serch_user_name(userr_name,user_password,dict = bank):
    bool1 = check_user_name(userr_name)
    if bool1 : #用户存在
        bool2 = match_user_name_and_user_password(userr_name,user_password) #用户名密码是否匹配
        if bool2 : #如果用户名密码匹配
            idx = serchUserIdxByUser_name(userr_name) #找到对应的字典索引
            user_name = dict[idx]["user_name"]
            user_password = dict[idx]["user_password"]
            balance = dict[idx]["balance"]
            address = dict[idx]["address"]
            bank = dict[idx]["bank"]
            print(f"当前帐号:{user_name},\n密码:{user_password},\n余额:{balance},\n用户居住地址:{address},\n当前开户行:{bank}")